{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/14_legends.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "## Add builtin legends from geemap Python package\n",
    "\n",
    "https://github.com/gee-community/geemap/blob/master/geemap/legends.py\n",
    "\n",
    "### Available builtin legends:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "legends = geemap.builtin_legends\n",
    "for legend in legends:\n",
    "    print(legend)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "### Available Land Cover Datasets in Earth Engine\n",
    "\n",
    "https://developers.google.com/earth-engine/datasets/tags/landcover"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "### National Land Cover Database (NLCD)\n",
    "\n",
    "https://developers.google.com/earth-engine/datasets/catalog/USGS_NLCD_RELEASES_2019_REL_NLCD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map.add_basemap(\"HYBRID\")\n",
    "landcover = ee.Image(\"USGS/NLCD_RELEASES/2019_REL/NLCD/2019\").select(\"landcover\")\n",
    "Map.addLayer(landcover, {}, \"NLCD Land Cover\")\n",
    "Map.add_legend(builtin_legend=\"NLCD\", title=\"NLCD Land Cover\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8",
   "metadata": {},
   "source": [
    "### National Wetlands Inventory (NWI)\n",
    "\n",
    "https://www.fws.gov/program/national-wetlands-inventory/wetlands-mapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map.add_basemap(\"HYBRID\")\n",
    "Map.add_basemap(\"FWS NWI Wetlands\")\n",
    "Map.add_legend(builtin_legend=\"NWI\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "### MODIS Land Cover Type Yearly Global 500m\n",
    "\n",
    "https://developers.google.com/earth-engine/datasets/catalog/MODIS_051_MCD12Q1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map.add_basemap(\"HYBRID\")\n",
    "\n",
    "landcover = ee.Image(\"MODIS/051/MCD12Q1/2013_01_01\").select(\"Land_Cover_Type_1\")\n",
    "\n",
    "Map.setCenter(6.746, 46.529, 2)\n",
    "Map.addLayer(landcover, {}, \"MODIS Land Cover\")\n",
    "Map.add_legend(builtin_legend=\"MODIS/051/MCD12Q1\")\n",
    "\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12",
   "metadata": {},
   "source": [
    "## Add customized legends for Earth Engine data\n",
    "\n",
    "There are three ways you can add customized legends for Earth Engine data\n",
    "\n",
    "1. Define legend keys and colors\n",
    "2. Define legend dictionary\n",
    "3. Convert Earth Engine class table to legend dictionary\n",
    "\n",
    "### Define legend keys and colors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "legend_keys = [\"One\", \"Two\", \"Three\", \"Four\", \"etc\"]\n",
    "# colorS can be defined using either hex code or RGB (0-255, 0-255, 0-255)\n",
    "legend_colors = [\"#8DD3C7\", \"#FFFFB3\", \"#BEBADA\", \"#FB8072\", \"#80B1D3\"]\n",
    "# legend_colors = [(255, 0, 0), (127, 255, 0), (127, 18, 25), (36, 70, 180), (96, 68 123)]\n",
    "\n",
    "Map.add_legend(keys=legend_keys, colors=legend_colors, position=\"bottomleft\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14",
   "metadata": {},
   "source": [
    "### Define a legend dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "legend_dict = {\n",
    "    \"11 Open Water\": \"466b9f\",\n",
    "    \"12 Perennial Ice/Snow\": \"d1def8\",\n",
    "    \"21 Developed, Open Space\": \"dec5c5\",\n",
    "    \"22 Developed, Low Intensity\": \"d99282\",\n",
    "    \"23 Developed, Medium Intensity\": \"eb0000\",\n",
    "    \"24 Developed High Intensity\": \"ab0000\",\n",
    "    \"31 Barren Land (Rock/Sand/Clay)\": \"b3ac9f\",\n",
    "    \"41 Deciduous Forest\": \"68ab5f\",\n",
    "    \"42 Evergreen Forest\": \"1c5f2c\",\n",
    "    \"43 Mixed Forest\": \"b5c58f\",\n",
    "    \"51 Dwarf Scrub\": \"af963c\",\n",
    "    \"52 Shrub/Scrub\": \"ccb879\",\n",
    "    \"71 Grassland/Herbaceous\": \"dfdfc2\",\n",
    "    \"72 Sedge/Herbaceous\": \"d1d182\",\n",
    "    \"73 Lichens\": \"a3cc51\",\n",
    "    \"74 Moss\": \"82ba9e\",\n",
    "    \"81 Pasture/Hay\": \"dcd939\",\n",
    "    \"82 Cultivated Crops\": \"ab6c28\",\n",
    "    \"90 Woody Wetlands\": \"b8d9eb\",\n",
    "    \"95 Emergent Herbaceous Wetlands\": \"6c9fb8\",\n",
    "}\n",
    "\n",
    "landcover = ee.Image(\"USGS/NLCD/NLCD2016\").select(\"landcover\")\n",
    "Map.addLayer(landcover, {}, \"NLCD Land Cover\")\n",
    "\n",
    "Map.add_legend(title=\"NLCD Land Cover Classification\", legend_dict=legend_dict)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16",
   "metadata": {},
   "source": [
    "### Convert an Earth Engine class table to legend\n",
    "\n",
    "For example: MCD12Q1.051 Land Cover Type Yearly Global 500m\n",
    "\n",
    "https://developers.google.com/earth-engine/datasets/catalog/MODIS_051_MCD12Q1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "ee_class_table = \"\"\"\n",
    "Value\tColor\tDescription\n",
    "0\t1c0dff\tWater\n",
    "1\t05450a\tEvergreen needleleaf forest\n",
    "2\t086a10\tEvergreen broadleaf forest\n",
    "3\t54a708\tDeciduous needleleaf forest\n",
    "4\t78d203\tDeciduous broadleaf forest\n",
    "5\t009900\tMixed forest\n",
    "6\tc6b044\tClosed shrublands\n",
    "7\tdcd159\tOpen shrublands\n",
    "8\tdade48\tWoody savannas\n",
    "9\tfbff13\tSavannas\n",
    "10\tb6ff05\tGrasslands\n",
    "11\t27ff87\tPermanent wetlands\n",
    "12\tc24f44\tCroplands\n",
    "13\ta5a5a5\tUrban and built-up\n",
    "14\tff6d4c\tCropland/natural vegetation mosaic\n",
    "15\t69fff8\tSnow and ice\n",
    "16\tf9ffa4\tBarren or sparsely vegetated\n",
    "254\tffffff\tUnclassified\n",
    "\"\"\"\n",
    "\n",
    "landcover = ee.Image(\"MODIS/051/MCD12Q1/2013_01_01\").select(\"Land_Cover_Type_1\")\n",
    "Map.setCenter(6.746, 46.529, 2)\n",
    "Map.addLayer(landcover, {}, \"MODIS Land Cover\")\n",
    "\n",
    "legend_dict = geemap.legend_from_ee(ee_class_table)\n",
    "Map.add_legend(title=\"MODIS Global Land Cover\", legend_dict=legend_dict)\n",
    "\n",
    "Map"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
